home *** CD-ROM | disk | FTP | other *** search
/ Aminet 7 / Aminet 7 - August 1995.iso / Aminet / misc / sci / RARS_Amiga_3.lha / RARS / TextField.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-27  |  5.6 KB  |  135 lines

  1. /*********************************************************************************************\
  2. *                                                                                             *
  3. * TextField.c - A simple example source that demonstrates how to implement custom             *
  4. *     images in EAGUI. This source should be included in another source, since it             *
  5. *     does not contain headers and needs some support routines, that are usually              *
  6. *     supplied by a parent environment.                                                       *
  7. *                                                                                             *
  8. * This source is placed in the public domain.                                                 *
  9. *                                                                                             *
  10. * Use a tab size of 5, and a right border of 95 if possible.                                  *
  11. *                                                                                             *
  12. \*********************************************************************************************/
  13.  
  14. /***********************************
  15.  
  16. TODO:
  17.  
  18.  - Clean up this example code.
  19.  - Make the structure more universal, so more render methods can be written in a
  20.    similar form. This could then be the start of a PD library, which contains many
  21.    custom image objects for various uses.
  22.  - Add a raytracing render method :-)
  23.  
  24. ***********************************/
  25.  
  26. /* Information that is needed by this object, but that isn't maintained by EAGUI itself. */
  27. typedef struct ci_TextField
  28. {
  29.     STRPTR            tf_string_ptr;        /* string that is displayed */
  30.     struct TextAttr *    tf_textattr_ptr;    /* font that is used */
  31.     ULONG            tf_flags;            /* different flags */
  32.     UBYTE            tf_frontpen;        /* front pen to use */
  33. };
  34. /* Alternative alignment flags. If these aren't specified, the default is to
  35.  * center the textfield both horizontally and vertically.
  36.  */
  37. #define CITF_ALIGNLEFT    0x00000001
  38. #define CITF_ALIGNRIGHT    0x00000002
  39. #define CITF_ALIGNTOP    0x00000004
  40. #define CITF_ALIGNBOTTOM    0x00000008
  41.  
  42. STATIC struct IntuiText itext = {
  43.     1, 0,    /* frontpen, backpen */
  44.     JAM1,    /* drawmode */
  45.     0, 0,    /* left, top offset */
  46.     NULL,    /* textattr */
  47.     NULL,    /* text */
  48.     NULL};    /* next intuitext */
  49.  
  50. /*********************************************************************************************\
  51. *                                                                                             *
  52. * MinSize Method                                                                              *
  53. *                                                                                             *
  54. \*********************************************************************************************/
  55. ULONG meth_MinSize_TextField(struct Hook *hook_ptr, struct ea_Object *obj_ptr, APTR msg_ptr)
  56. {
  57.     ULONG                minwidth, minheight;
  58.     struct ci_TextField *    tf_ptr;
  59.  
  60.     /* get a pointer to our structure, and check if we actually got it */
  61.     if (tf_ptr = (struct ci_TextField *)ea_GetAttr(obj_ptr, EA_UserData))
  62.     {
  63.         /* now, we use the library to determine the dimensions of the string */
  64.         minwidth = ea_TextLength(tf_ptr->tf_textattr_ptr, tf_ptr->tf_string_ptr, 0);
  65.         minheight = ea_TextHeight(tf_ptr->tf_textattr_ptr);
  66.  
  67.         /* and finally, we set these values */
  68.         ea_SetAttr(obj_ptr, EA_MinWidth, minwidth);
  69.         ea_SetAttr(obj_ptr, EA_MinHeight, minheight);
  70.     }
  71.  
  72.     /* we always return success */
  73.     return(0);
  74. }
  75.  
  76. /*********************************************************************************************\
  77. *                                                                                             *
  78. * Render Method                                                                               *
  79. *                                                                                             *
  80. \*********************************************************************************************/
  81. ULONG meth_Render_TextField(struct Hook *hook_ptr, struct ea_Object *obj_ptr, struct ea_RenderMessage *rm_ptr)
  82. {
  83.     struct ci_TextField *    tf_ptr;
  84.     ULONG                minwidth, minheight, width, height;
  85.     ULONG                left, top;
  86.  
  87.     /* get a pointer to our structure, and check if we actually got it */
  88.     if (tf_ptr = (struct ci_TextField *)ea_GetAttr(obj_ptr, EA_UserData))
  89.     {
  90.         /* get sizes of the object */
  91.         ea_GetAttrs(obj_ptr,
  92.             EA_MinWidth,        &minwidth,
  93.             EA_MinHeight,        &minheight,
  94.             EA_Width,            &width,
  95.             EA_Height,        &height,
  96.             TAG_DONE);
  97.  
  98.         /* get offsets of object relative to root (window) */
  99.         left = ea_GetObjectLeft(rm_ptr->root_ptr, obj_ptr);
  100.         top = ea_GetObjectTop(rm_ptr->root_ptr, obj_ptr);
  101.  
  102.         /* now align the object */
  103.         if (tf_ptr->tf_flags & CITF_ALIGNRIGHT)
  104.         {
  105.             left += (width - minwidth);
  106.         }
  107.         else if (!(tf_ptr->tf_flags & CITF_ALIGNLEFT))
  108.         {
  109.             left += (width - minwidth) / 2;
  110.         }
  111.         if (tf_ptr->tf_flags & CITF_ALIGNBOTTOM)
  112.         {
  113.             top += (height - minheight);
  114.         }
  115.         else if (!(tf_ptr->tf_flags & CITF_ALIGNTOP))
  116.         {
  117.             top += (height - minheight) / 2;
  118.         }
  119.  
  120.         /* and finally render it */
  121.         itext.ITextFont = tf_ptr->tf_textattr_ptr;
  122.         itext.IText = tf_ptr->tf_string_ptr;
  123.         itext.FrontPen = tf_ptr->tf_frontpen;
  124.         PrintIText(rm_ptr->rastport_ptr, &itext, left, top);
  125.     }
  126.     /* return success */
  127.     return(0);
  128. }
  129.  
  130. /*********************************************************************************************\
  131. *                                                                                             *
  132. * The end!                                                                                    *
  133. *                                                                                             *
  134. \*********************************************************************************************/
  135.